/***
 * 将转义符换成普通字符串
 * @param str
 * @returns
 */
function escape2Html(s) {
	return $("<div/>").html(s).text();
}
function htmlEscape2Text(s){
	return $("<div/>").text(s).html();
}
/**
 * 值表工具
 */
function getValueByKey(data, dictName, key) {
	if (data.hasOwnProperty(dictName)) {
		var list = this.options.data[dictName];
		var length = list.length;
		for (var i = 0; i < length; i++) {
			if (list[i]["id"] == key) {
				return escape2Html(list[i]["descrip"]);
			}
		}
		return "";
	}
}

;
var dict={};
(function() {
	var dictManager = function(ele, opt) {
		this.$element = ele;
		this.defaults = {
			data : null
		};
		this.options = $.extend({}, this.defaults, opt);
	}
	dictManager.prototype = {
			/***
			 * 筛选显示值中包含传入值的选项
			 * @param dictName
			 * @param pid
			 * @param hasAll
			 * @param hasEmpty
			 * @param allValue
			 * @param allKey
			 * @param likeValue 筛选条件
			 */
		getValueLikeV : function(dictName, pid, hasAll, hasEmpty, allValue,
				allKey, likeValue) {
			var _obj = this.$element.get(0);// select element
			if( !_obj){
				console.log("该页面元素不存在，dictName:"+dictName);
				return;
			}
			if (this.options.data.hasOwnProperty(dictName)) {
				if (_obj != undefined) {
					_obj.options.length = 0;
					if (hasAll) {
						if (allValue != undefined)
							_obj.options.add(new Option("" + allValue, ""));
						else if (allValue != undefined && allKey != undefined)
							_obj.options.add(new Option("" + allValue, ""
									+ allKey));
						else
							_obj.options.add(new Option("全部", ""));
					}
					if (hasEmpty) {
						_obj.options.add(new Option("", ""));
					}
				}
				var list = this.options.data[dictName];
				var length = list.length;
				for (var i = 0; i < length; i++) {
					if (pid != -1 && list[i].hasOwnProperty("pid")
							&& list[i]["pid"] == pid) {
						if (list[i]["descrip"].indexOf(likeValue) != -1) {
							_obj.options
									.add(new Option("" + escape2Html(list[i]["descrip"]), ""
											+ list[i]["id"]));
						}
						continue;
					}
					if (pid == -1) {
						if (list[i]["descrip"].indexOf(likeValue) != -1) {
							_obj.options
									.add(new Option("" + escape2Html(list[i]["descrip"]), ""
											+ list[i]["id"]));
						}
					}
				}
			}
		},
		/***
		 * 筛选隐藏ID值为传入值的选项
		 * @param dictName
		 * @param pid
		 * @param hasAll
		 * @param hasEmpty
		 * @param allValue
		 * @param allKey
		 * @param likeKeyValue 筛选key
		 */
		getValueLikeK : function(dictName, pid, hasAll, hasEmpty, allValue,
				allKey, likeKeyValue) {
			var _obj = this.$element.get(0);// select element
			if( !_obj){
				console.log("该页面元素不存在，dictName:"+dictName);
				return;
			}
			console.log("likeKeyValue:"+likeKeyValue+"--"+!likeKeyValue)
			if(!likeKeyValue){
				console.log("缺少Key匹配值，无法使用！dictName"+dictName);
				return;
			}
			if (this.options.data.hasOwnProperty(dictName)) {
				if (_obj != undefined) {
					_obj.options.length = 0;
					if (hasAll) {
						if (allValue != undefined)
							_obj.options.add(new Option("" + allValue, ""));
						else if (allValue != undefined && allKey != undefined)
							_obj.options.add(new Option("" + allValue, ""
									+ allKey));
						else
							_obj.options.add(new Option("全部", ""));
					}
					if (hasEmpty) {
						_obj.options.add(new Option("", ""));
					}
				}
				var list = this.options.data[dictName];
				var length = list.length;
				for (var i = 0; i < length; i++) {
					if (pid != -1 && list[i].hasOwnProperty("pid")
							&& list[i]["pid"] == pid) {
						if (list[i]["id"].indexOf(likeKeyValue) != -1) {
							_obj.options
									.add(new Option("" + escape2Html(list[i]["descrip"]), ""
											+ list[i]["id"]));
						}
						continue;
					}
					if (pid == -1) {
						if (list[i]["id"].indexOf(likeKeyValue) != -1) {
							_obj.options
									.add(new Option("" + escape2Html(list[i]["descrip"]), ""
											+ list[i]["id"]));
						}
					}
				}
			}
		},
		/**
		 * 
		 * @param dictName
		 *            json-obj
		 * @param pid
		 *            根ID
		 * @param hasAll
		 *            是否显示全部
		 * @param hasEmpty
		 *            是否显示空
		 * @param allValue
		 *            显示全部时对应文字
		 * @param allKey
		 *            显示全部时对应key
		 */
		initStaticDict : function(dictName, pid, hasAll, hasEmpty, allValue,
				allKey) {
			var _obj = this.$element.get(0);// select element
			if( !_obj){
				console.log("该页面元素不存在，dictName:"+dictName);
				return;
			}
			if (this.options.data.hasOwnProperty(dictName)) {
				if (_obj != undefined) {
					/*_obj.options.length = 0;*/
					if (hasAll) {
						if (allValue != undefined)
							_obj.options.add(new Option("" + allValue, ""));
						else if (allValue != undefined && allKey != undefined)
							_obj.options.add(new Option("" + allValue, ""
									+ allKey));
						else
							_obj.options.add(new Option("全部", ""));
					}
					if (hasEmpty) {
						_obj.options.add(new Option("", ""));
					}
				}
				var list = this.options.data[dictName];
				var length = list.length;
				for (var i = 0; i < length; i++) {
					if (pid != -1 && list[i].hasOwnProperty("pid")
							&& list[i]["pid"] == pid) {
						_obj.options.add(new Option("" + escape2Html(list[i]["descrip"]), ""
								+ list[i]["id"]));
						continue;
					}
					if (pid == -1) {
						_obj.options.add(new Option("" + escape2Html(list[i]["descrip"]), ""
								+ list[i]["id"]));
					}
				}
			}
		},
		setHtmlValue : function(dictName, key) {
			var $obj = this.$element;// select element
			if (this.options.data.hasOwnProperty(dictName)) {
				if ($obj.length>0) {
					var list = this.options.data[dictName];
					var length = list.length;
					for (var i = 0; i < length; i++) {
						if( list[i]["id"] == key){
							$obj.html(list[i]["descrip"]);
						}
					}
				}
			}
		},
		setInputValue : function(dictName, key) {
			var $obj = this.$element;// select element
			if (this.options.data.hasOwnProperty(dictName)) {
				if ($obj.length>0) {
					var list = this.options.data[dictName];
					var length = list.length;
					for (var i = 0; i < length; i++) {
						if( list[i]["id"] == key){
							$obj.val(list[i]["descrip"]);
						}
					}
				}
			}
		}
	}
	$.fn.dictManager = function(opt) {
		var dict = new dictManager(this, opt);
		return dict;
	}
})();